package com.atguigu.tingshu.search.service.impl;

import com.atguigu.tingshu.album.client.AlbumInfoFeignClient;
import com.atguigu.tingshu.album.client.CategoryFeignClient;
import com.atguigu.tingshu.common.constant.RedisConstant;
import com.atguigu.tingshu.common.execption.GuiguException;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.model.album.AlbumInfo;
import com.atguigu.tingshu.model.album.BaseCategoryView;
import com.atguigu.tingshu.search.service.ItemService;
import com.atguigu.tingshu.user.client.UserInfoFeignClient;
import com.atguigu.tingshu.vo.album.AlbumStatVo;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

@Slf4j
@Service
@SuppressWarnings({"all"})
public class ItemServiceImpl implements ItemService {

    @Autowired
    private AlbumInfoFeignClient albumInfoFeignClient;

    @Autowired
    private CategoryFeignClient categoryFeignClient;

    @Autowired
    private UserInfoFeignClient userInfoFeignClient;

    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;

    @Autowired
    private RedissonClient redissonClient;

    @Override
    public Map<String, Object> getItem(Long albumId) {
        //创建map集合
        Map<String,Object> map = new HashMap<>();
        //  将专辑Id添加到布隆过滤器;
        /*RBloomFilter<Object> bloomFilter = redissonClient.getBloomFilter(RedisConstant.ALBUM_BLOOM_FILTER);
        if (!bloomFilter.contains(albumId)) {
            log.error("专辑Id不存在{}", albumId);
            return map;
        }*/
        //多线程查询数据
        CompletableFuture<AlbumInfo> albumInfoCompletableFuture = CompletableFuture.supplyAsync(()->{
            //远程调用
            Result<AlbumInfo> infoResult = albumInfoFeignClient.getAlbumInfo(albumId);
            Assert.notNull(infoResult,"查询专辑信息失败");
            AlbumInfo albumInfo = infoResult.getData();
            Assert.notNull(albumInfo,"查询专辑信息失败");
            map.put("albumInfo",albumInfo);
            //返回数据
            return albumInfo;
        },threadPoolExecutor).whenCompleteAsync((v,t)->{
            System.out.println("专辑标题：\t" + v.getAlbumTitle());
            if(t != null){
                log.error("查询专辑数据失败：{}",t.getMessage());
            }
        });
        //获取分类数据
        CompletableFuture<Void> baseCategoryCompletableFuture = albumInfoCompletableFuture.thenAcceptAsync(albumInfo->{
            //远程调用
            Result<BaseCategoryView> baseCategoryViewResult = categoryFeignClient.getCategoryView(albumInfo.getCategory3Id());
            Assert.notNull(baseCategoryViewResult,"查询数据分类失败");
            BaseCategoryView baseCategoryView = baseCategoryViewResult.getData();
            Assert.notNull(baseCategoryView,"查询数据分类失败");
            //存储数据
            map.put("baseCategoryView",baseCategoryView);
        },threadPoolExecutor).whenCompleteAsync((v,t)->{
            if(t != null){
                log.error("查询分类数据失败",t.getMessage());
            }
        });
        //获取用户Id
        CompletableFuture<Void> userComplateAsync = albumInfoCompletableFuture.thenAcceptAsync(albumInfo->{
            Result<UserInfoVo> userInfoVoResult = userInfoFeignClient.getUserInfoVo(albumInfo.getUserId());
            Assert.notNull(userInfoVoResult,"查询作者信息失败");
            UserInfoVo userInfoVo = userInfoVoResult.getData();
            Assert.notNull(userInfoVo,"查询作者信息失败");
            map.put("announcer",userInfoVo);
        },threadPoolExecutor).whenCompleteAsync((v,t)->{
            if(t != null){
                log.error("查询作者信息失败：{}",t.getMessage());
            }
        });
        //根据专辑Id查询专辑统计数据
        CompletableFuture<Void> statComplateAsync = CompletableFuture.runAsync(()->{
            //远程调用
            Result<AlbumStatVo> albumStatVoResult = albumInfoFeignClient.getAlbumStatVo(albumId);
            Assert.notNull(albumStatVoResult,"查询专辑统计数据失败");
            AlbumStatVo albumStatVo = albumStatVoResult.getData();
            Assert.notNull(albumStatVo,"获取专辑统计数据失败");
            map.put("albumStatVo",albumStatVo);
        },threadPoolExecutor).whenCompleteAsync((v,t)->{
            if(t != null){
                log.error("查询统计数据失败：{}",t.getMessage());
            }
        });
        try {
            CompletableFuture.allOf(albumInfoCompletableFuture,baseCategoryCompletableFuture,userComplateAsync,statComplateAsync).join();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return map;
    }
}
